package Card;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Poker {
    private static final int TOTAL_CARDS = 54;

    private Deck deckA;

    private Deck deckB;

    private Deck deckC;

    private Random rand;

    public Poker() {
        this.deckA = new Deck();
        this.deckB = new Deck();
        this.deckC = new Deck();

        this.rand = new Random();
    }

    public Deck getDeckA() {
        return this.deckA;
    }

    public Deck getDeckB() {
        return this.deckB;
    }

    public Deck getDeckC() {
        return this.deckC;
    }

    public void deal() {
        this.rand.setSeed(System.currentTimeMillis());

        deckA.reset();
        deckB.reset();
        deckC.reset();

        List<Integer> cache = new ArrayList<>();
        for (; cache.size() < Poker.TOTAL_CARDS; ) {
            int num = rand.nextInt(TOTAL_CARDS);
            if (Poker.contains(cache, num))
                continue;
            cache.add(num);
        }

        for (int i = 0; i < Poker.TOTAL_CARDS / 3; i++) {
            int num = cache.get(i);
            Poker.deal(deckA, num);
        }

        for (int i = 18; i < 2 * Poker.TOTAL_CARDS / 3; i++) {
            int num = cache.get(i);
            Poker.deal(deckB, num);
        }

        for (int i = 36; i < Poker.TOTAL_CARDS; i++) {
            int num = cache.get(i);
            Poker.deal(deckC, num);
        }
    }

    private static boolean contains(List<Integer> cache, int num) {
        return cache.contains(num);
    }

    public static void deal(Deck deck, int num) {
        if (num < TOTAL_CARDS - 2)
            deck.add(Card.Flush.fromValue(num % 4), num / 4 + 1);
        else {
            if (num == TOTAL_CARDS - 2)
                deck.add(Card.Flush.JOKER, 0);
            else
                deck.add(Card.Flush.JOKER, 1);
        }
    }
}
